package com.whut.mobilesafe.activity;

import android.content.Intent;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.view.View;
import android.widget.Toast;

import com.whut.mobilesafe.R;
import com.whut.mobilesafe.utils.PrefUtils;
import com.whut.mobilesafe.view.SettingItemView;

/**
 * 作    者:yubo.xiaoyubo
 * 时    间：2015/12/22 20:18
 * 版    本：1.0
 * 描    述： 这里绑定sim卡的意思是 获取sim 卡序列号，然后把他保存起来
 */
public class Step2Activity extends BaseStepActivity {

    private SettingItemView sivBindSim;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setup2);
        sivBindSim = (SettingItemView) findViewById(R.id.siv_bind_sim);
        String bindSim = PrefUtils.getString(this, "bind_sim");
        if (!TextUtils.isEmpty(bindSim)) {
            sivBindSim.setChecked(true);
        } else {
            sivBindSim.setChecked(false);
        }

        sivBindSim.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean checked = sivBindSim.isChecked();
                if (checked) {
                    sivBindSim.setChecked(false);
                    PrefUtils.setString(getApplicationContext(), "bind_sim", null);
                    Toast.makeText(Step2Activity.this, "取消绑定sim", Toast.LENGTH_SHORT).show();
                } else {
                    sivBindSim.setChecked(true);
                    String simSerialNumber = getSimSerialNumber();
                    PrefUtils.setString(getApplicationContext(), "bind_sim", simSerialNumber);
                    Toast.makeText(Step2Activity.this, "sim卡绑定成功", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    @Override
    public void showNext() {
        String bindSim = PrefUtils.getString(this, "bind_sim");
        if(!TextUtils.isEmpty(bindSim)){
            startActivity(new Intent(getApplicationContext(), Step3Activity.class));
            finish();
            overridePendingTransition(R.anim.anim_right_in, R.anim.anim_right_out);
        }else {
            Toast.makeText(this, "您的sim 卡还未绑定", Toast.LENGTH_SHORT).show();
            return;
        }
    }

    @Override
    public void showPrevious() {
        startActivity(new Intent(getApplicationContext(), Step1Activity.class));
        finish();
        overridePendingTransition(R.anim.anim_left_in, R.anim.anim_left_out);
    }

    public String getSimSerialNumber() {
        TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
        String simSerialNumber = tm.getSimSerialNumber();
        return simSerialNumber;
    }
}

